package com.coda.app.service.impl;

import com.coda.app.mapper.*;
import com.coda.app.service.ApartmentService;
import com.coda.app.service.ViewAppointmentService;
import com.coda.appVo.apartment.ApartmentItemVo;
import com.coda.appVo.appointment.AppointmentDetailVo;
import com.coda.appVo.appointment.AppointmentItemVo;
import com.coda.appVo.graph.GraphVo;
import com.coda.context.BaseContext;
import com.coda.entity.ApartmentInfo;
import com.coda.entity.LabelInfo;
import com.coda.entity.ViewAppointment;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class ViewAppointmentServiceImpl implements ViewAppointmentService {

    @Autowired
    private ViewAppointmentMapper viewAppointmentMapper;
    @Autowired
    private ApartmentMapper apartmentMapper;
    @Autowired
    private GraphMapper graphMapper;
    @Autowired
    private LabelMapper labelMapper;
    @Autowired
    private RoomMapper roomMapper;


    /**
     * 保存或更新看房预约
     * @param viewAppointment
     */
    @Override
    public void saveOrUpdate(ViewAppointment viewAppointment) {

        viewAppointment.setUserId(BaseContext.getCurrentId());
        viewAppointment.setAppointmentStatus(1);
        if (viewAppointment.getId() == null ){
            viewAppointmentMapper.insert(viewAppointment);
        }else {
            viewAppointmentMapper.update(viewAppointment);
        }
    }

    /**
     * 查询个人预约看房列表
     * @return
     */
    @Override
    public List<AppointmentItemVo> getList() {
        List<ViewAppointment> appointmentList = viewAppointmentMapper.getList(BaseContext.getCurrentId());
        List<AppointmentItemVo> appointmentItemVos = new ArrayList<>();

        for (ViewAppointment viewAppointment : appointmentList) {
            AppointmentItemVo appointmentItemVo = new AppointmentItemVo();
            //通过id获取公寓信息，赋值公寓名称
            ApartmentInfo apartment = apartmentMapper.getApartmentById(viewAppointment.getApartmentId());
            appointmentItemVo.setApartmentName(apartment.getName());

            //获取公寓图片
            List<GraphVo> graph = graphMapper.getGraph(apartment.getId(), 1);
            appointmentItemVo.setGraphVoList(graph);

            appointmentItemVo.setId(viewAppointment.getId());
            appointmentItemVo.setAppointmentStatus(viewAppointment.getAppointmentStatus());
            appointmentItemVo.setAppointmentTime(viewAppointment.getAppointmentTime());

            appointmentItemVos.add(appointmentItemVo);
        }
        return appointmentItemVos;
    }

    /**
     * 根据id查询公寓详细信息
     * @param id
     * @return
     */
    @Override
    public AppointmentDetailVo getDetailById(Long id) {
        //获取看房预约信息
        ViewAppointment viewAppointment = viewAppointmentMapper.getById(id);
        //赋值
        AppointmentDetailVo appointmentDetailVo = new AppointmentDetailVo();
        BeanUtils.copyProperties(viewAppointment,appointmentDetailVo);

        //获取公寓标签信息
        List<LabelInfo> apartmentLabel = labelMapper.getApartmentLabel(viewAppointment.getApartmentId());
        //获取公寓图片信息
        List<GraphVo> graph = graphMapper.getGraph(viewAppointment.getApartmentId(), 1);
        //获取最小金额
        BigDecimal min = roomMapper.getMin(viewAppointment.getApartmentId());
        //获取公寓信息
        ApartmentInfo apartment = apartmentMapper.getApartmentById(viewAppointment.getApartmentId());
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();

        BeanUtils.copyProperties(apartment,apartmentItemVo);
        apartmentItemVo.setMinRent(min);
        apartmentItemVo.setLabelInfoList(apartmentLabel);
        apartmentItemVo.setGraphVoList(graph);

        appointmentDetailVo.setApartmentItemVo(apartmentItemVo);

        return appointmentDetailVo;
    }
}
